package cn.wbu.zyh.service.impl;

import cn.wbu.zyh.constant.MessageConstant;
import cn.wbu.zyh.constant.StatusConstant;
import cn.wbu.zyh.dto.EmployeeDTO;
import cn.wbu.zyh.dto.LoginDTO;
import cn.wbu.zyh.dto.EmployeePageQueryDTO;
import cn.wbu.zyh.entity.Employee;
import cn.wbu.zyh.exception.AccountLockedException;
import cn.wbu.zyh.exception.PasswordErrorException;
import cn.wbu.zyh.mapper.EmployeeMapper;
import cn.wbu.zyh.result.PageResult;
import cn.wbu.zyh.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import cn.wbu.zyh.exception.AccountNotFoundException;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    public Employee login(LoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO){
        //开始分页查询
        long total = 0;
        List<Employee> records = new ArrayList<>();
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        if(employeePageQueryDTO.getHasTeam()==3){
            Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
            total = page.getTotal();
            records = page.getResult();
        }else if(employeePageQueryDTO.getHasTeam() == 0){
            Page<Employee> page = employeeMapper.pageQueryHasTeam(employeePageQueryDTO);
            total = page.getTotal();
            records = page.getResult();
        }
        return new PageResult(total,records);
    }

    public void startOrStop(Integer status, Long id){
        Employee employee = Employee.builder().status(status).id(id).build();
        employeeMapper.update(employee);
    }

    @Override
    public void update(EmployeeDTO employeeDTO){
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        //使用Md5加密
        String password = DigestUtils.md5DigestAsHex(employeeDTO.getPassword().getBytes());

        employee.setPassword(password);
        employeeMapper.update(employee);
        employeeMapper.updateRole(employee);
    }

    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO,employee);

        //设置账号的状态，默认正常状态
        //使用Md5加密
        String password = DigestUtils.md5DigestAsHex(employeeDTO.getPassword().getBytes());
        employee.setPassword(password);
        employee.setStatus(StatusConstant.ENABLE);
        employeeMapper.insert(employee);
        employeeMapper.insertRole(employee.getUsername(),employee.getRole());
    }

    @Override
    public void register(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO,employee);
        //使用Md5加密
        String password = DigestUtils.md5DigestAsHex(employeeDTO.getPassword().getBytes());
        employee.setPassword(password);
        //设置账号的状态，默认正常状态
        employee.setStatus(StatusConstant.ENABLE);
        employee.setCreatedTime(LocalDateTime.now());
        employee.setRole("USER");
        employeeMapper.insert(employee);
        employeeMapper.insertRole(employee.getUsername(),employee.getRole());
    }
}
